package com.xingda.packing.dmss.http;

import com.alibaba.fastjson.JSONObject;
import com.xingda.packing.dmss.config.appset.AppSetUrl;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 扭转值处理请求接口
 * <p>
 * (目前双钱的工字轮轮)从工字轮查询接口里查不出扭转值，需要根据查询的pkvlaue,再去请求接口获得扭转值
 *
 * @author LEGION
 */
public class WheelTorsionValueHttp {

    /**
     * 查询扭转值
     */
    public static BigDecimal getTorsionValue(List<String> pkValues) {

        BigDecimal torsionValue = null;
        if (Objects.isNull(pkValues) || pkValues.isEmpty()) {
            return null;
        }
        for (int i = 0; i < pkValues.size(); i++) {
            String pkValue = pkValues.get(pkValues.size() - i - 1);
            List<NameValuePair> list = Collections.singletonList(new BasicNameValuePair("id", pkValue));
            try {
                JSONObject entityJson = BaseHttp.get(AppSetUrl.wheel_torsion_url, list);
                if (entityJson.getIntValue("code") == 0 && "success".equals(entityJson.getString("msg"))) {
                    JSONObject result = entityJson.getJSONObject("result");
                    if (Objects.nonNull(result) && !result.isEmpty()) {
                        String docName = result.getString("docname");
                        String[] nz = docName.split("\\|");
                        if (nz.length >= 2 && "nz".equalsIgnoreCase(nz[0])) {
                            torsionValue = new BigDecimal(nz[1]);
                            break;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return torsionValue;
    }
}
